﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.ComponentModel;
using System.Globalization;
using System.Web.Configuration;

namespace HospitalSystem
{
    public partial class Vision : System.Web.UI.Page
    {
        public static List<string> x = new List<string>();
        DataTable dt = new DataTable();
        DataTable dx = new DataTable();
    
        protected void Page_Load(object sender, EventArgs e)
        {



            String ShowValue = (string)Session["CID"];
            Label1.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            Label3.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            Label2.Text = ShowValue3;


            String ShowValue4 = (string)Session["CaseNo"];
            Label4.Text = ShowValue4;

            if (!IsPostBack)
            {
                BindData();
            }
        }
        protected void BindData()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            x.Clear();
            scon.Open();
            SqlCommand cmd = new SqlCommand("select Name from SightProblem", scon);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read() != false)
            {
                x.Add(reader["Name"].ToString());
            } 
            reader.Close();
            dx.Columns.Add("Name");

            for (int i = 0; i < x.Count(); i++)
            {
                dx.Rows.Add();
                dx.Rows[i]["Name"] = x[i].ToString();
            }
            GridView1.DataSource = dx;
            GridView1.DataBind();
            scon.Close();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);


            if(dt.Columns.Count <= 0)
            {
            dt.Columns.Add("CID");
            dt.Columns.Add("CaseNo");
            dt.Columns.Add("SightEvalCode");
            dt.Columns.Add("RecorderID");
            dt.Columns.Add("EvalDateTime");
            }
            foreach (GridViewRow row in GridView1.Rows)
            { 
                scon.Open();
                SqlCommand cmd = new SqlCommand("select SightEvalCode from SightProblem where Name = @sj", scon);
                cmd.Parameters.AddWithValue("@sj", GridView1.Rows[row.RowIndex].Cells[0].Text);
               
                CheckBox cb = (CheckBox)row.FindControl("ProbSelector");
                if (cb != null && cb.Checked)
                {
                    SqlDataReader reader2 = cmd.ExecuteReader();
                    if (reader2.Read())
                    {
                        dt.Rows.Add(Label1.Text, Label4.Text, reader2["SightEvalCode"].ToString(), Label5.Text, DateTime.Now.ToString());
                    }
                    else 
                    {
                    }
                }
                scon.Close();
            }
            GridView2.DataSource = dt;
            GridView2.DataBind();




            SqlCommand command2 = new SqlCommand("delete from [SightEvaluation] where CID=@CID and CaseNo=@CaseNo", scon);
            scon.Open();
            command2.Parameters.AddWithValue("@CID", Label1.Text);
            command2.Parameters.AddWithValue("@CaseNo", Label4.Text);
            command2.ExecuteNonQuery();
            scon.Close();



            for (int i = 0; i < dt.Rows.Count; i++)
            {

                SqlCommand command = new SqlCommand("INSERT INTO [SightEvaluation] (CID,CaseNo,SightEvalCode,RecorderID,EvalDateTime) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["SightEvalCode"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                scon.Open();

                command.ExecuteNonQuery();
                scon.Close();



                SqlCommand command3 = new SqlCommand("INSERT INTO [SightEvaluation_log] (CID,CaseNo,SightEvalCode,RecorderID,EvalDateTime) VALUES('" + dt.Rows[i]["CID"].ToString() + "','" + dt.Rows[i]["CaseNo"].ToString() + "','" + dt.Rows[i]["SightEvalCode"].ToString() + "','" + dt.Rows[i]["RecorderID"].ToString() + "','" + dt.Rows[i]["EvalDateTime"].ToString() + "')", scon);
                scon.Open();

                command3.ExecuteNonQuery();
                scon.Close();
            }




            crepsnumber();
            psnumbertolabel();
            creDetails();
        }











        protected void crepsnumber()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (HostID,StaffID,PSDateTime) VALUES(@HostID,@StaffID,@PSDateTime)", scon);
            scon.Open();
            //command2.Parameters.AddWithValue("@PSNumber", Label1.Text);
            command2.Parameters.AddWithValue("@HostID", hostid.Text);
            command2.Parameters.AddWithValue("@StaffID", staffid.Text);
            command2.Parameters.AddWithValue("@PSDateTime", DateTime.Now);

            command2.ExecuteNonQuery();
            scon.Close();
        }


        protected void psnumbertolabel()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand CmdSql47 = new SqlCommand("SELECT PSNumber FROM [ServiceProvision] WHERE PSNumber = (SELECT MAX(PSNumber)  FROM [ServiceProvision])", scon);
            scon.Open();

            SqlDataReader reader = CmdSql47.ExecuteReader();
            // DataTable dt = new DataTable();
            //  dt.Load(reader);\
            // = reader;

            if (reader.Read())
            {
                psnumber.Text = reader["PSNumber"].ToString();


            }
            else
            {

            }
            scon.Close();
            reader.Close();


            Session["PSNumber01"] = psnumber.Text;
        }


        protected void creDetails()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            
            SqlCommand creDetails = new SqlCommand("INSERT INTO [ServiceProvisionVsPerson] (CID,CaseNo,PSNumber) VALUES(@CID,@CaseNo,@PSNumber)", scon);
            scon.Open();

            creDetails.Parameters.AddWithValue("@CID", Label1.Text);
            creDetails.Parameters.AddWithValue("@CaseNo", Label4.Text);
            creDetails.Parameters.AddWithValue("@PSNumber", psnumber.Text);


            creDetails.ExecuteNonQuery();
            scon.Close();


            SqlCommand command = new SqlCommand("INSERT INTO [ServiceProvisionDetails] (ACTCode,PSNumber,logDateTime) VALUES('A0201000',@PSNumber,@logDateTime)", scon);
            scon.Open();
            command.Parameters.AddWithValue("@PSNumber", psnumber.Text);
            command.Parameters.AddWithValue("@logDateTime", DateTime.Now);
            command.ExecuteNonQuery();
            scon.Close();






        }















    }

}